﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
	<%
		String sortIds = request.getParameter("sortIds");
		String baseUrl = request.getContextPath();
	%>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript">
		$(document).ready(function(){
			var baseParams = {sortIds:<%=sortIds%>};
			var EXT_QUERY_TIME_OUT = 1200000;
			var sortListStore;
            var sortListGrid;
            
            // 工具栏
		    var toolbar = new Ext.Toolbar({
		    	renderTo: 'sortListGridDiv',
		    	items: [
				    new Ext.Button({
					    id: 'sort-save-button',
						text: '保存',
						iconCls: 'tick'
					}),
					new Ext.Button({
					    id: 'sort-cancel-button',
						text: '取消',
						iconCls: 'cancel'
					})
				]
			});
            
			sortListStore = new Ext.data.JsonStore({
		        url: '${pageContext.request.contextPath}/getSuperviseObj.action',
		        root: 'datas',
				timeout : EXT_QUERY_TIME_OUT,
		        fields: ['id','supervise','supervise_target','target_name'],
		        baseParams: baseParams,
		        idProperty:'supervise_target',//唯一标识(如果不设定唯一标识,则多行记录只会显示一行记录)
		        autoLoad: true
		    });
		    
		    var Menu = new Ext.menu.Menu({ 
            items: [{ 
            text: '上移', 
            iconCls: 'arrow_up',  
            handler: function(){ 
                if(rowIndex == 0) { 
                    return; 
                } 
                var data = sortListGrid.store.data.items[rowIndex].data; 
                var record = new Ext.data.Record({ 
                        id:data.id, 
                        supervise: data.supervise, 
                        supervise_target: data.supervise_target,
                        target_name: data.target_name
                }); 
                sortListGrid.getStore().removeAt(rowIndex);  
                sortListGrid.getStore().insert(rowIndex - 1, record);  
                sortListGrid.getSelectionModel().selectRow(rowIndex - 1);  
                sortListGrid.getView().refresh();  
            }, 
            scope: this 
        }, 
        { 
            text: '下移', 
            iconCls: 'arrow_down',  
            handler: function(){ 
                if(rowIndex < sortListGrid.getStore().getCount() - 1){ 
                    var data = sortListGrid.store.data.items[rowIndex].data; 
                    var record = new Ext.data.Record({ 
                        id:data.id, 
                        supervise: data.supervise, 
                        supervise_target: data.supervise_target,
                        target_name: data.target_name
                    }); 
                    sortListGrid.getStore().removeAt(rowIndex);
                    sortListGrid.getStore().insert(rowIndex + 1, record);  
                    sortListGrid.getSelectionModel().selectRow(rowIndex + 1);  
                    sortListGrid.getView().refresh();        
                } 
            }, 
            scope: this 
          }] 
       });
		    
                // 用户数据表格
			    sortListGrid = new Ext.grid.GridPanel({
				id: 'sortListGrid',
				renderTo: 'sortListGridDiv',
				border: false,
				stateful: true,
			    autoScroll: 'auto',
		        store: sortListStore,
		        loadMask: true,
		        cm: new Ext.grid.ColumnModel({
		            defaults: {
		                width: 100,
		                sortable: true
		            },
			        columns: [
						new Ext.grid.RowNumberer({header:'№',width:50}),
						{id:'id',header: '传报编号', width: 200, hidden: true, sortable: false, dataIndex: 'id'},
						{id:'supervise',header: '工单类型', width: 150, hidden: true, sortable: false, dataIndex: 'supervise'},
			            {id:'supervise_target',header: '告警级别编号', width: 200,hidden: true, sortable: false, dataIndex: 'supervise_target'},
			            {id:'target_name',header: '督办对象', width: 200, sortable: false, dataIndex: 'target_name'}	
					]
		        }),
		        columnLines: true,
		        listeners: { 
                 rowdblclick: function(sortListGrid, rowIndex, event) { 
            }, 
            contextmenu:function(e){ 
                var target = e.getTarget();  
                e.stopEvent() ; 
                Menu.showAt(e.getXY()); 
                var view = sortListGrid.getView(); 
                rowIndex = view.findRowIndex(target); 
                sortListGrid.getSelectionModel().selectRow(rowIndex); 
            }
            }
		    });
		    
		    // 设置Grid高度和宽度
		    Anynote.resizeGridTo("sortListGrid", 0, 56);
		    
		    // 保存按钮
		    $("#sort-save-button").click(function(){
		            var sortParams;
		            var id = '';
					var superviseTarget = '';
					var targetName = '';
		            //获取grid的记录条数
				    var count = sortListGrid.getStore().getCount();
				    var girdcount=0;
		            for(var i=0;i<count;i++){
						var data = sortListGrid.getStore().getAt(i).data;//data就是对应record的一个一个的对象
						id = data.id;
						if(girdcount == count - 1){
						superviseTarget = superviseTarget + data.supervise_target;
						targetName = targetName + data.target_name;
						}else{
						    superviseTarget = superviseTarget + data.supervise_target+',';
							targetName = targetName + data.target_name+',';
						}
						girdcount=girdcount+1;
					}
					sortParams = {id:id,superviseTarget:superviseTarget,targetName:targetName};
					// 发送请求
				    Anynote.ajaxRequest({
					baseUrl: '<%=baseUrl %>',
					baseParams: sortParams,
					action: '${pageContext.request.contextPath}/updateSuperviseId.action',
					callback: function(jsonResult){
						handleListGrid.getStore().reload();
						sortHandleWindow.close();
					},
						showWaiting: true,
						failureMsg: '保存失败.'
				    });
			});
		    
		 	// 取消按钮
		    $("#sort-cancel-button").click(function(){
		    	sortHandleWindow.close();
			});
		});
	</script>
</head>
<body> 
<div id="sortListDiv" style="width:100%;height:100%;">
<div id="sortListGridDiv"></div>
</div>
</body>
</html>